<script setup lang="ts">
import type {
  SysLogErrorProcessingQo,
  SysLogErrorVo,
} from '@/typings/system/log/error'
import {
  processingLogErrApi,
} from '@/api/system/log/error'

interface OnCloseResolveType extends CloseResolveType {}
let onCloseResolve = function (_value: OnCloseResolveType) { }

const dialogRef = $ref<DialogInstance>()
const ruleFormRef = ref<FormInstance>()
const dialogModal = $ref<SysLogErrorProcessingQo>({})
let confirmLoading = $ref(false)
const dialogLoading = $ref(false)

const rules = reactive<FormRules<SysLogErrorProcessingQo>>({

})

// 打开
async function showDialog(args: {
  data: SysLogErrorVo
}) {
  const dialogRes = dialogRef!.openDialog<OnCloseResolveType>({
    title: '处理异常日志',
    size: 'medium',
  })

  ruleFormRef.value?.clearValidate()
  onCloseResolve = dialogRes.onCloseResolve

  Object.assign(dialogModal, args.data)

  if (dialogModal.processingResult === null) {
    dialogModal.processingResult = ''
  }

  return await dialogRes.dialogPrm
}

// 确认
async function confirm(formEl: FormInstance | undefined) {
  if (!formEl)
    return
  await formEl.validate(async (valid) => {
    if (!valid) {
      return
    }
    confirmLoading = true

    await processingLogErrApi(dialogModal).finally(() => {
      confirmLoading = false
    })

    Message({
      type: 'success',
      message: `处理成功`,
    })
    onCloseResolve({ type: true })
  })
}

// 取消
function cancel() {
  ruleFormRef.value?.clearValidate()
  onCloseResolve({ type: false })
}

defineExpose({ showDialog })
</script>

<template>
  <sy-dialog
    ref="dialogRef"
    :before-close="cancel"
    :confirm-loading="confirmLoading"
    :disabled-confirm="dialogLoading"
    @confirm="confirm(ruleFormRef)"
  >
    <el-form
      ref="ruleFormRef"
      v-loading="dialogLoading"
      :model="dialogModal"
      :rules="rules"
      label-width="auto"
      status-icon
    >
      <el-form-item label="状态" prop="processStatus">
        <sy-dict
          v-model="dialogModal.processStatus"
          dict-type="log_err_process_status"
        />
      </el-form-item>
      <el-form-item label="结果" prop="processingResult">
        <sy-md-edit v-model="dialogModal.processingResult" />
      </el-form-item>
    </el-form>
  </sy-dialog>
</template>

<style scoped></style>
